using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    public struct BusMap
    {
        public int iABit;
        public int iBBit;
        public int iWidth;
        public bool bLSB;
    }

    public class PipelineReMap
    {
        PipelineBus oBusA;
        PipelineBus oBusB;
        List<BusMap> lMap;

        public PipelineReMap(PipelineBus poBusA, PipelineBus poBusB)
        {
            oBusA = poBusA;
            oBusB = poBusB;
            lMap = new List<BusMap>();
        }

        public void DefineMap(int piAMSB, int piWidth, int piBMSB, bool pbLSB)
        {
            BusMap oMap = new BusMap();
            oMap.iABit = piAMSB;
            oMap.iWidth = piWidth;
            oMap.iBBit = piBMSB;
            oMap.bLSB = pbLSB;
            lMap.Add(oMap);
            //no duplicate check, no time   
        }

        public void CompleteMap()
        {
            CompleteMap(false);
        }

        public void CompleteMap(bool pbConnectDataOnly)
        {
            foreach (BusMap oMap in lMap)
            {
                if (oMap.bLSB)
                {
                    oBusA.Data.ConnectSubset(oMap.iABit, oMap.iABit + oMap.iWidth - 1, oBusB.Data, oMap.iBBit, oMap.iBBit + oMap.iWidth - 1);                    
                }
                else
                {
                    oBusA.Data.ConnectSubset(oMap.iABit, oMap.iABit - oMap.iWidth + 1, oBusB.Data, oMap.iBBit, oMap.iBBit - oMap.iWidth + 1);
                }
            }
            if (!pbConnectDataOnly)
            {
                oBusA.DataReady.Join(oBusB.DataReady);
                oBusA.Stall.Join(oBusB.Stall);
            }
        }
    }
}
